import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:industry_net/base/common/fluwx_util.dart';
import 'package:industry_net/base/common/route/routes.dart';
import 'package:industry_net/base/common/util.dart';
import 'package:industry_net/base/dioUtil/http_man.dart';
import 'package:industry_net/pages/login/main/plugin/ali_login.dart';
import 'package:sign_in_with_apple/sign_in_with_apple.dart';
import 'state.dart';
import 'package:industry_net/pages/me/main/models/usually.dart';

class LoginController extends GetxController {
  final state = LoginState();

  @override
  void onInit() {
    super.onInit();
    obseverEvent();
    setupLoginTypes();
  }

  void obseverEvent() {
    state.registerGesture.onTap = () {
      Get.toNamed(RouterPath.register);
    };
    state.forgotGesture.onTap = () {
      Get.toNamed(RouterPath.forgot_pwd);
    };
  }

  void setupLoginTypes() async {
    state.loginTypes = [
      UsuallyModel(url: 'icon_phone.png', name: '一键登录', onTap: fastLogin),
      UsuallyModel(url: 'icon_code.png', name: '验证码登录', onTap: codeLogin),
    ];

    if (await WXUtil().isWxInstalled) {
      state.loginTypes.add(
          UsuallyModel(url: 'icon_wechat.png', name: '微信登录', onTap: wxLogin));
    }
    if (GetPlatform.isIOS) {
      state.loginTypes.add(
        UsuallyModel(url: 'icon_apple.png', name: '苹果登录', onTap: appleLogin),
      );
    }
    update();
  }

  void login() {
    EasyLoading.showToast('定位');
    // Get.back();
  }

  //一键登录
  void fastLogin() async {
    AliLogin().login();
  }

  //验证码登录
  void codeLogin() {
    state.isCodeLogin.value = !state.isCodeLogin.value;
  }

  //微信登录
  void wxLogin() {
    WXUtil().login();
  }

  void appleLogin() async {
    final credential = await SignInWithApple.getAppleIDCredential(
      scopes: [
        AppleIDAuthorizationScopes.email,
        AppleIDAuthorizationScopes.fullName,
      ],
      webAuthenticationOptions: WebAuthenticationOptions(
        // TODO: Set the `clientId` and `redirectUri` arguments to the values you entered in the Apple Developer portal during the setup
        clientId: 'de.lunaone.flutter.signinwithappleexample.service',

        redirectUri:
            // For web your redirect URI needs to be the host of the "current page",
            // while for Android you will be using the API server that redirects back into your app via a deep link
            Uri.parse(
          'https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple',
        ),
      ),
      // TODO: Remove these if you have no need for them
      nonce: 'example-nonce',
      state: 'example-state',
    );

    // ignore: avoid_print
    print(credential);

    // This is the endpoint that will convert an authorization code obtained
    // via Sign in with Apple into a session in your system
    final signInWithAppleEndpoint = Uri(
      scheme: 'https',
      host: 'flutter-sign-in-with-apple-example.glitch.me',
      path: '/sign_in_with_apple',
      queryParameters: <String, String>{
        'code': credential.authorizationCode,
        if (credential.givenName != null) 'firstName': credential.givenName!,
        if (credential.familyName != null) 'lastName': credential.familyName!,
        'useBundleId': GetPlatform.isIOS ? 'true' : 'false',
        if (credential.state != null) 'state': credential.state!,
      },
    );
    final session = await HttpMan().requestUri(signInWithAppleEndpoint);
    // If we got this far, a session based on the Apple ID credential has been created in your system,
    // and you can now set this as the app's session
    // ignore: avoid_print
    print(session);
  }

  @override
  void onClose() {
    state.registerGesture.dispose();
    state.forgotGesture.dispose();
    super.onClose();
  }

  @override
  void dispose() {
    super.dispose();
  }
}
